﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using ARR.Entities;
using DDF.Entity;
using ARR.UI.CategoriesService;

namespace ARR.UI.Models
{
    public interface ICategoryMatchMasterModel : IModelBase
    {
        ObservableCollection<CategoryMatch> NacsCategoryMatches { get; set; }

        ObservableCollection<CategoryMatch> OtherCatogoryMatches { get; set; }

        ObservableCollection<Category> Categories { get; set; }


        void Save();

        void LoadCategoryMatches();
    }

    public class CategoryMatchMasterModel : ModelBase, ICategoryMatchMasterModel
    {
        public CategoryMatchMasterModel()
        {
            NacsCategoryMatches = new ObservableCollection<CategoryMatch>();
            OtherCatogoryMatches = new ObservableCollection<CategoryMatch>();
            Categories = new ObservableCollection<Category>();

            LoadCategories();

            LoadCategoryMatches();
        }


        public ObservableCollection<CategoryMatch> NacsCategoryMatches { get; set; }

        public ObservableCollection<CategoryMatch> OtherCatogoryMatches { get; set; }

        public ObservableCollection<Category> Categories { get; set; }


        public void Save()
        {
            List<CategoryMatch> toSave = OtherCatogoryMatches.Where(x => x.GetEntityState() != EntityState.Original).ToList();
            toSave.AddRange(NacsCategoryMatches.Where(x => x.GetEntityState() != EntityState.Original));

            toSave.ForEach(x => x.CountryId = WorkspaceData.Instance.Country.Id);

            CategoriesServiceClient service = new CategoriesServiceClient();

            try
            {
                service.SaveCategoryMatchesMaster(toSave.ToList());
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }
        }

        public void LoadCategoryMatches()
        {
            List<CategoryMatch> matches = new List<CategoryMatch>();
            CategoriesServiceClient service = new CategoriesServiceClient();

            try
            {
                matches = service.GetCategoryMatches(WorkspaceData.Instance.Country.Id);
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }

            foreach (CategoryMatch match in matches)
            {
                match.ChangeState(EntityState.Original);
            }

            NacsCategoryMatches = new ObservableCollection<CategoryMatch>(matches.Where(m => m.SourceCategoryId.HasValue));
            OtherCatogoryMatches = new ObservableCollection<CategoryMatch>(matches.Where(m => !string.IsNullOrWhiteSpace(m.Name)));

        }

        private void LoadCategories()
        {
            CategoriesServiceClient service = new CategoriesServiceClient();

            List<Category> categories = new List<Category>();

            try
            {
                categories = service.GetByLevels(2);
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }

            foreach (var category in categories.Where(x => x.Level == 2))
            {
                category.SubCategories = new List<Category>();
                category.SubCategories.AddRange(categories.Where(x => x.ParentId == category.Id));
            }
            Categories = new ObservableCollection<Category>(categories.Where(x => x.Level == 2).OrderBy(x => x.Name));
        }
    }

    public class CategoryMatchMasterModelMock : MockModelBase, ICategoryMatchMasterModel
    {
        public CategoryMatchMasterModelMock()
        {
            NacsCategoryMatches = new ObservableCollection<CategoryMatch>();
            OtherCatogoryMatches = new ObservableCollection<CategoryMatch>();
            Categories = new ObservableCollection<Category>();

            LoadCategories();
            LoadCategoryMatches();
        }

        public ObservableCollection<CategoryMatch> NacsCategoryMatches { get; set; }

        public ObservableCollection<CategoryMatch> OtherCatogoryMatches { get; set; }

        public ObservableCollection<Category> Categories { get; set; }


        public void Save()
        {

        }

        public void LoadCategoryMatches()
        {
            OtherCatogoryMatches.Add(new CategoryMatch { SourceCategory = Categories[0], SourceCategoryId = Categories[0].Id, TargetCategory = Categories[1], CategoryId = Categories[1].Id });
            OtherCatogoryMatches.Add(new CategoryMatch { SourceCategory = Categories[2], SourceCategoryId = Categories[2].Id, TargetCategory = Categories[3], CategoryId = Categories[3].Id });

            OtherCatogoryMatches.Add(new CategoryMatch { Name = "Cat0015", TargetCategory = Categories[1], CategoryId = Categories[1].Id });
            OtherCatogoryMatches.Add(new CategoryMatch { Name = "Cat004", TargetCategory = Categories[2], CategoryId = Categories[2].Id });
        }

        private void LoadCategories()
        {
            Categories.Add(new Category { Id = 1, Code = "Cat01", Name = "Categoria 1", Level = 1, Convention = "NACS" });
            Categories.Add(new Category { Id = 2, Code = "Cat02", Name = "Categoria 2", Level = 1, Convention = "NACS" });
            Categories.Add(new Category { Id = 3, Code = "Cat03", Name = "Categoria 3", Level = 1, Convention = "NACS" });
            Categories.Add(new Category { Id = 4, Code = "Cat04", Name = "Categoria 4", Level = 1, Convention = "NACS" });
            Categories.Add(new Category { Id = 5, Code = "Cat05", Name = "Categoria 5", Level = 1, Convention = "NACS" });
        }
    }

}
